﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dashboard.ProductBonus.Business;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace Dashboard.ProductBonus
{
    public partial class FormTimeCardList : Form
    {
        public string StaffNo { get; set; }
        public DateTime Date { get; set; }

        public FormTimeCardList()
        {
            InitializeComponent();
        }

        private void toolStripButtonQuery_Click(object sender, EventArgs e)
        {
            this.DoQuery();
        }

        private void toolStripButtonExport_Click(object sender, EventArgs e)
        {
            var dialogResult = this.saveFileDialogMain.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                var fileName = this.saveFileDialogMain.FileName;
                this.ultraGridExcelExporterMain.Export(this.ultraGridMain, fileName);
                MessageBox.Show("Export data successfully!", "Information", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormTimeCardList_Load(object sender, EventArgs e)
        {
            this.DoQuery();
        }

        private void DoQuery()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                AttendanceReport ar = new AttendanceReport();

                DataTable result = ar.GetStaffTimeCardList(this.Date.AddDays(-1), this.Date.AddDays(1), this.StaffNo);

                this.ultraGridMain.DataSource = result;
                this.ultraGridMain.DataBind();

                if (this.ultraGridMain.Rows.Count > 0)
                {
                    this.toolStripButtonExport.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void ultraGridMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.None;
            // 是否允许调整列宽
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            // 是否允许移动栏位顺序
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortSingle;

            // 不允许删除\更新
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;
            e.Layout.Override.AllowUpdate = DefaultableBoolean.False;

            e.Layout.Override.CellClickAction = CellClickAction.RowSelect;

            // 滚动提示
            e.Layout.Bands[0].ScrollTipField = "StaffNo";
        }
    }
}
